package comchifaner.util;

public class JsonResult {
	private int code;
	private Object data;
	private String message;

	public JsonResult(int code, String message, Object data) {
		this.code = code;
		this.message = message;
		this.data = data;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String toString() {
		return JsonUtil.getJsonString(this);
	}

	public static String result(int code, String message) {
		return JsonUtil.getJsonString(new JsonResult(code, message, ""));
	}

	public static String result(int code, String message, Object data) {
		return JsonUtil.getJsonString(new JsonResult(code, message, data));
	}

	public static String emptySuccess() {
		return JsonUtil.getJsonString(new JsonResult(ResultCode.SUCCESS.getValue(), Tips.INVOKE_SUCCESS, ""));
	}

	/**
	 * 普通单条数据封装
	 *@category
	 *@param data
	 *@return
	 *@createDate 2016年3月10日
	 */
	public static String successWithData(Object data) {
		return JsonUtil.getJsonString(new JsonResult(ResultCode.SUCCESS.getValue(), Tips.INVOKE_SUCCESS, data));
	}
	
	public static String seccessDataByPage(){
		return null;
	}

	public static String success(String tips, Object data) {
		return JsonUtil.getJsonString(new JsonResult(ResultCode.SUCCESS.getValue(), tips, data));
	}

	public static String emptyFailure() {
		return JsonUtil.getJsonString(new JsonResult(ResultCode.FAILURE.getValue(), Tips.INVOKE_FAILURE, ""));
	}
	
	public static String parmsFailure() {
		return JsonUtil.getJsonString(new JsonResult(ResultCode.INVALID_PARMS.getValue(), Tips.INVALID_PARMS, ""));
	}

	public static String failureWithTips(String tips) {
		return JsonUtil.getJsonString(new JsonResult(ResultCode.FAILURE.getValue(), tips, ""));
	}

	public static String failure(String tips, Object data) {
		return JsonUtil.getJsonString(new JsonResult(ResultCode.FAILURE.getValue(), tips, data));
	}

	public static String notLogin() {
		return JsonUtil
				.getJsonString(new JsonResult(ResultCode.NOT_LOGIN.getValue(), ResultCode.NOT_LOGIN.getMessage(), ""));
	}

	public static enum ResultCode {
		SUCCESS(0, Tips.INVOKE_SUCCESS), FAILURE(1, Tips.INVOKE_FAILURE), NO_DATA(2, "没有数据"), // 订单已支付等待处理
		EXIST(3, "已存在"), // 订单已支付等待处理
		REPEAT(4, "数据重复"), // 重复
		ACTIVATE(5, "未激活"), // 用户未激活
		INVALID_PARMS(6, Tips.INVALID_PARMS), NOT_LOGIN(100, Tips.NOT_LOGIN);

		private int state;
		private String message;

		// 构造函数，枚举类型只能为私有
		private ResultCode(int state, String message) {
			this.state = state;
			this.message = message;
		}

		public int getValue() {
			return state;
		}

		public String getMessage() {
			return this.message;
		}
	}

}
